﻿using System;
using System.Data;
using System.Collections.Generic;
using Chenxin.Lib.Hr.Entity;
using Chenxin.Lib.Utility;
namespace Chenxin.Lib.Hr.BLL
{
    /// <summary>
    /// 业务逻辑类Hr_VacationRecord 的摘要说明。
    /// </summary>
    public class Hr_VacationRecord
    {
        
        public Hr_VacationRecord()
        { }
        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Chenxin.Lib.Hr.Entity.Hr_VacationRecord model)
        {
            Chenxin.Lib.Hr.DAL.Hr_VacationRecord.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Chenxin.Lib.Hr.Entity.Hr_VacationRecord model)
        {
            Chenxin.Lib.Hr.DAL.Hr_VacationRecord.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(Guid RecordId)
        {

            Chenxin.Lib.Hr.DAL.Hr_VacationRecord.Delete(RecordId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Chenxin.Lib.Hr.Entity.Hr_VacationRecord GetModel(Guid RecordId)
        {

            return Chenxin.Lib.Hr.DAL.Hr_VacationRecord.GetModel(RecordId);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return Chenxin.Lib.Hr.DAL.Hr_VacationRecord.GetList(strWhere);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord> GetModelList(string strWhere)
        {
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_VacationRecord.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord> DataTableToList(DataTable dt)
        {
            List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord> modelList = new List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Chenxin.Lib.Hr.Entity.Hr_VacationRecord model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Chenxin.Lib.Hr.Entity.Hr_VacationRecord();
                    if (dt.Rows[n]["RecordId"].ToString() != "")
                    {
                        model.RecordId = new Guid(dt.Rows[n]["RecordId"].ToString());
                    }
                    if (dt.Rows[n]["UserId"].ToString() != "")
                    {
                        model.UserId = new Guid(dt.Rows[n]["UserId"].ToString());
                    }
                    model.UserName = dt.Rows[n]["UserName"].ToString();
                    if (dt.Rows[n]["DepartId"].ToString() != "")
                    {
                        model.DepartId = new Guid(dt.Rows[n]["DepartId"].ToString());
                    }
                    model.DepartName = dt.Rows[n]["DepartName"].ToString();
                    if (dt.Rows[n]["DateTime"].ToString() != "")
                    {
                        model.DateTime = DateTime.Parse(dt.Rows[n]["DateTime"].ToString());
                    }
                    if (dt.Rows[n]["StartTime"].ToString() != "")
                    {
                        model.StartTime = DateTime.Parse(dt.Rows[n]["StartTime"].ToString());
                    }
                    if (dt.Rows[n]["EndTime"].ToString() != "")
                    {
                        model.EndTime = DateTime.Parse(dt.Rows[n]["EndTime"].ToString());
                    }
                    if (dt.Rows[n]["Days"].ToString() != "")
                    {
                        model.Days = decimal.Parse(dt.Rows[n]["Days"].ToString());
                    }
                    model.VacationType = dt.Rows[n]["VacationType"].ToString();
                    model.Reason = dt.Rows[n]["Reason"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        //*******************************************
        //功能概要       ：根据参数列表获得用户信息一览
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：无
        //功能詳細       ：根据参数列表获得用户信息一览
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-05
        //*******************************************
        public DataSet GetModelListByCoditions(System.Collections.Hashtable hsParams)
        {
            //select文
            string strSelect;
            //order文
            string strOrder = "";
            //当前页数
            int intCurPage = 1;
            //显示行数
            int intRowCnt = 10;
            //where文
            System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
            strWhere.Append(" 1=1 ");
            //权限
            string strPermit;

            //权限
            if (hsParams.ContainsKey("Permit"))
            {
                strPermit = hsParams["Permit"].ToString();
                if (strPermit == Constants.Permit_ViewAllVacation)
                {
                    //全部
                }
                else if (strPermit == Constants.Permit_ViewCityVacation)
                {
                    //strWhere.AppendFormat(" And CityId = '{0}'", hsParams["CurCityId"]);
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Hr_DepartmentInfo M WHERE Hr_VacationRecord.DepartId=M.DepartId And M.CityId= '{0}'", hsParams["CurCityId"]);
                    strWhere.Append(") ");
                }
                else if (strPermit == Constants.Permit_ViewCompanyVacation)
                {
                    //strWhere.AppendFormat(" And CompanyId= '{0}'", hsParams["CurCompanyId"]);
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Hr_DepartmentInfo M WHERE Hr_VacationRecord.DepartId=M.DepartId And M.CompanyId= '{0}'", hsParams["CurCompanyId"]);
                    strWhere.Append(") ");
                }
                else if (strPermit == Constants.Permit_ViewDepartVacation)
                {
                    strWhere.Append(" And EXISTS(");
                    strWhere.AppendFormat("Select 1 from Struct_UserDepartRelation M WHERE Hr_VacationRecord.DepartId=M.DepartId And M.Userid= '{0}' And M.ViewVacation=1 ", hsParams["CurUserId"]);
                    strWhere.Append(") ");
                }
            }
            //权限为当前用户
            else if (hsParams.ContainsKey("CurUserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["CurUserId"] + "'");
            }

            //用户名
            if (hsParams.ContainsKey("UserId"))
            {
                strWhere.Append(" And UserId = '" + hsParams["UserId"] + "'");
            }
            //部门
            else if (hsParams.ContainsKey("DepartId"))
            {
                strWhere.Append(" And DepartId = '" + hsParams["DepartId"] + "'");
            }
            //商圈
            else if (hsParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo entArea = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, hsParams["AreaId"]);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                entArea = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    entArea = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(hsParams["AreaId"].ToString()));
                    if (entArea != null) cs.Set(strCachkey, entArea, DateTime.Now.AddDays(1));
                }

                if (entArea != null)
                {
                    strWhere.AppendFormat(" AND (DepartId in (select departId from Hr_DepartmentInfo where DepartSrc like '{0}%'))", entArea.DepartSrc);
                }
            }
            //公司
            else if (hsParams.ContainsKey("CompanyId"))
            {
                strWhere.AppendFormat(" AND (DepartId in (Select DepartId from Hr_DepartmentInfo where CompanyId = '{0}'))", hsParams["CompanyId"]);
            }
            //城市
            else if (hsParams.ContainsKey("CityId"))
            {
                strWhere.AppendFormat(" AND (DepartId in (Select DepartId from Hr_DepartmentInfo where CityId = '{0}'))", hsParams["CityId"]);
            }

            //年,月份
            if (hsParams.ContainsKey("Year"))
            {
                strWhere.Append(" And year(Datetime) = " + hsParams["Year"].ToString());
                strWhere.Append(" And month(Datetime) = " + hsParams["Month"].ToString());
            }
            //排序项目
            if (hsParams.ContainsKey("sidx"))
            {
                strOrder = strOrder + hsParams["sidx"].ToString();
            }
            //排序方式
            if (hsParams.ContainsKey("sord"))
            {
                strOrder = strOrder + " " + hsParams["sord"].ToString();
            }

            //当前页数
            if (hsParams.ContainsKey("page"))
            {
                intCurPage = Convert.ToInt32(hsParams["page"]);
            }
            //显示行数
            if (hsParams.ContainsKey("rows"))
            {
                intRowCnt = Convert.ToInt32(hsParams["rows"]);
            }

            strSelect = " RecordId,UserId,UserName,DepartId,DepartName,DateTime,StartTime,EndTime,Days,VacationType,Reason ";

            string TableFieldList = "RecordId,UserId,UserName,DepartId,DepartName,DateTime,StartTime,EndTime,Days,VacationType,Reason";

            //开始范围
            int intSelStart;
            //结束范围
            int intSelEnd;

            intSelStart = (intCurPage - 1) * intRowCnt + 1;
            intSelEnd = intCurPage * intRowCnt;
            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_VacationRecord.GetList("Hr_VacationRecord", strSelect, TableFieldList, strWhere.ToString(), strOrder, intCurPage, intRowCnt, true);
            
            return ds;
        }

        //*******************************************
        //功能概要       ：得到总件数
        //参数説明       ：hsParams 参数列表
        //返回值説明     ：记录总件数
        //功能詳細       ：得到总件数
        //作成者         ：he.jifeng 
        //作成日         ：2010-03-07
        //*******************************************
        public int getTotalCnt(System.Collections.Hashtable hsParams)
        {
            //where文
            string strWhere = " 1=1 ";
            //用户ID
            if (hsParams.ContainsKey("UserId"))
            {
                strWhere = strWhere + " And UserId = '" + hsParams["UserId"].ToString() + "' ";
            }else if (hsParams.ContainsKey("DepartId"))
            {
                //部门ID
                strWhere = strWhere + " And DepartId = '" + hsParams["DepartId"].ToString() + "' ";
            }

            //年,月份
            if (hsParams.ContainsKey("Year"))
            {
                strWhere = strWhere + " And year(Datetime) = " + hsParams["Year"].ToString();
                strWhere = strWhere + " And month(Datetime) = " + hsParams["Month"].ToString();
            }

            DataSet ds = Chenxin.Lib.Hr.DAL.Hr_VacationRecord.GetList("Hr_VacationRecord", "", "", strWhere, "", 0, 0, false);

            //取得总记录数
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            }
            else
            {
                return 0;
            }
        }

        #endregion  成员方法
    }
}

